package com.liu.community.server.impl;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 16:47 2021/10/4
 */

import com.liu.community.mapper.UserMapper;
import com.liu.community.pojor.User;
import com.liu.community.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryUser() {
        return userMapper.queryUser();
    }

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public User queryUserByToken(String token) {
        User user = userMapper.queryUserByToken(token);
        return user;
    }

    @Override
    public User queryUserByAccountId(String accountId) {
        return  userMapper.queryUserByAccountId(accountId);
    }

    @Override
    public User queryUserById(Integer id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public void createOrUpdateUser(User user) {
        User dbUser = userMapper.queryUserByAccountId(user.getAccountId());
        if (dbUser != null){
            //修改
            dbUser.setGmtModifiled(System.currentTimeMillis());
            dbUser.setAvatarUrl(user.getAvatarUrl());
            dbUser.setName(user.getName());
            dbUser.setToken(user.getToken());
            dbUser.setId(dbUser.getId());
            userMapper.updateUser(dbUser);
        }else {
            //添加
            user.setGmtCreate(System.currentTimeMillis());
            user.setGmtModifiled(user.getGmtCreate());
            userMapper.insert(user);
        }
    }
}
